(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.ratingTypeQaAdjController', viewController);
	viewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ratingTypeService' ];
	function viewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, ratingTypeService) {
		var ctrl = this, params;
		ctrl.dialogsModule = dialogsModule;
		params = ctrl.dialogsModule.params;
		
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.formName = 'qaAdjPopupForm';
		ctrl.ok = function() {
			if($scope.qaAdjPopupForm) {
				$scope.qaAdjPopupForm.$setSubmitted();
				if(!$scope.qaAdjPopupForm.$valid) {
					dialogsModule.showErrorMessages($scope.qaAdjPopupForm);
					return;
				}
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl) !== false) {
				$uibModalInstance.close();
			}
		};
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		ctrl.init = function() {
			ctrl.ratingQaAdj = {};
			if(ctrl.validatorConfig && ctrl.validatorConfig.ratingQaAdjs) ctrl.layoutConfig.formConfigs.ratingQaAdjds.tableConfigs.table.validatorConfig = ctrl.validatorConfig.ratingQaAdjs.ratingQaAdjds;
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.ratingQaAdjds.tableConfigs.table, rowSelectionChangedOfQaAdjd);
			ctrl.innerGridOptions.data = 'vm.ratingQaAdj.ratingQaAdjds';
			ctrl.innerGridOptions.minRowsToShow = 4;
			ratingTypeService.dropDownDataOptions().then(function(response){
				ctrl.yesOrNoOptions = response.yesOrNoOptions;
				ctrl.itemTypeOptions = response.itemTypeOptions;
				return authService.editQaAdj(params).then(function(response) {
					ctrl.ratingQaAdj.ratingType = params.ratingType;
					ctrl.ratingQaAdj = params;
					ctrl.ratingQaAdj.ratingQaAdjds = params.ratingQaAdjds || [];
				}, angular.noop);
			}, angular.noop);
			
		};
		ctrl.addQaAdjd = function() {
			if (!ctrl.ratingQaAdj.ratingQaAdjds) {
				ctrl.ratingQaAdj.ratingQaAdjds = [];
			}
			var item = {
				ratingQachoiceId : '',
				ratingQaitemId : '',
				choiceDesc : '',
				choiceScore : '',
				dispOrder : ''
			};
			item.dataStatus = 'C';
			ctrl.ratingQaAdj.ratingQaAdjds.push(item);
		};

		ctrl.removeQaAdjd = function() {
			angular.forEach(ctrl.selectedQaAdjds, function(selectedItem, index) {
				angular.forEach(ctrl.ratingQaAdj.ratingQaAdjds, function(item, i) { 
					if (selectedItem.$$hashKey == item.$$hashKey) { 
						if('C' == selectedItem.dataStatus){
							ctrl.ratingQaAdj.ratingQaAdjds.splice(i, 1);
						} else {
							ctrl.ratingQaAdj.ratingQaAdjds[i].dataStatus = 'D';
						}
					}
				});
			});
		};
		ctrl.editQaAdjd = function() {
			if (!ctrl.selectedQaAdjds || ctrl.selectedQaAdjds.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.ratingType.qaAdj',
				'params' : ctrl.selectedQaAdjds[0],
				'btnCallback' : {
					'ok' : function(buCtrl) {
					}
				}
			});
		};
		ctrl.ratingQaAdjdChanged = function (obj, form) {
			if(form.$dirty && 'C' != obj.dataStatus) obj.dataStatus = 'U';
		}
		function rowSelectionChangedOfQaAdjd(eventRow, selectedRows) {
			ctrl.selectedQaAdjds = selectedRows;
		}
	}
})(angular);